<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>在线用户</span>
          <el-button type="danger" @click="handleForceLogout">
            <el-icon><SwitchButton /></el-icon>
            强退
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="登录地址" prop="ipaddr">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入登录地址"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="danger" @click="handleForceLogout">
            <el-icon><SwitchButton /></el-icon>
            强退
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="onlineList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="会话编号" align="center" prop="tokenId" width="200" />
        <el-table-column label="登录名称" align="center" prop="userName" />
        <el-table-column label="主机" align="center" prop="ipaddr" />
        <el-table-column label="登录地点" align="center" prop="loginLocation" />
        <el-table-column label="浏览器" align="center" prop="browser" />
        <el-table-column label="操作系统" align="center" prop="os" />
        <el-table-column label="登录时间" align="center" prop="loginTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button type="text" @click="handleForceLogout(scope.row)">
              <el-icon><SwitchButton /></el-icon>
              强退
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 遮罩层
const loading = ref(false)
// 选中数组
const ids = ref<string[]>([])
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 在线用户列表数据
const onlineList = ref<any[]>([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  ipaddr: '',
  userName: ''
})

// 查询在线用户列表
const getList = () => {
  loading.value = true
  // 模拟数据
  setTimeout(() => {
    onlineList.value = [
      {
        tokenId: 'token_001_20231201103000',
        userName: 'admin',
        ipaddr: '192.168.1.100',
        loginLocation: '内网IP',
        browser: 'Chrome',
        os: 'Windows 10',
        loginTime: '2023-12-01 10:30:00'
      },
      {
        tokenId: 'token_002_20231201091500',
        userName: 'user001',
        ipaddr: '192.168.1.101',
        loginLocation: '内网IP',
        browser: 'Firefox',
        os: 'macOS',
        loginTime: '2023-12-01 09:15:00'
      }
    ]
    total.value = 2
    loading.value = false
  }, 1000)
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryParams.ipaddr = ''
  queryParams.userName = ''
  handleQuery()
}

// 多选框选中数据
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.tokenId)
}

// 强退按钮操作
const handleForceLogout = (row?: any) => {
  const tokenIds = row?.tokenId || ids.value
  ElMessageBox.confirm('是否确认强退用户编号为"' + tokenIds + '"的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('强退成功')
    getList()
  })
}

// 刷新
const handleRefresh = () => {
  ElMessage.success('刷新成功')
  getList()
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

// 页码改变
const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mb8 {
  margin-bottom: 8px;
}
</style>
